local skel = fk.CreateSkill {
  name = "rmt__dianyi",
}

Fk:loadTranslationTable{
  ["rmt__dianyi"] = "点漪",
  [":rmt__dianyi"] = "出牌阶段限一次，或当你受到1点伤害后，你可以令手牌数与你相同的角色各摸一张牌，或弃置手牌数与你相同的角色各一张牌，然后将弃置牌中的装备牌置于你武将牌上，你可以分配其余的弃置牌。",

  ["#rmt__dianyi"] = "点漪：令手牌数与你相同的角色各摸一张牌或弃一张牌",
  ["rmt__dianyi_throw"] = "弃一张牌",
  ["#rmt__dianyi-throw"] = "点漪：请弃置 %dest 一张牌",
  ["#rmt__dianyi-give"] = "点漪：你可以分配这些牌",
  ["@@rmt__dianyi_tar"] = "手牌相同",

  ["$rmt__dianyi1"] = "夕照楼台正桃夭，湿飞双翠破涟漪。",
  ["$rmt__dianyi2"] = "智者能调、达人自玉，绿窗何妨？红楼在星期。",
}

---@param player ServerPlayer
local function doDianyi(player, choice)
  local room = player.room
  local targets = table.filter(room:getAlivePlayers(), function (p)
    return p:getHandcardNum() == player:getHandcardNum()
  end)
  room:doIndicate(player, targets)
  if choice == "draw1" then
    for _, p in ipairs(targets) do
      if not p.dead then
        p:drawCards(1, skel.name)
      end
    end
  else
    local cards = {}
    for _, p in ipairs(targets) do
      if not p.dead and not p:isNude() and not player.dead then
        local cid = room:askToChooseCard(player, { target = p, flag = "he", skill_name = skel.name,
        prompt = "#rmt__dianyi-throw::"..p.id})
        if cid then
          room:throwCard(cid, skel.name, p, player)
          table.insertIfNeed(cards, cid)
        end
      end
    end
    if player.dead then return end
    cards = table.filter(cards, function (id)
      return table.contains(room.discard_pile, id)
    end)
    if #cards == 0 then return end
    local equips = table.filter(cards, function (id)
      return Fk:getCardById(id).type == Card.TypeEquip
    end)
    if #equips > 0 and (player:hasSkill(skel.name, true) or player:hasSkill("rmt__chongshu", true)) then  --防止中途失去技能后一直挂在脸上
      player:addToPile(skel.name, equips, true, skel.name)
    end
    if player.dead then return end
    cards = table.filter(cards, function (id)
      return table.contains(room.discard_pile, id)
    end)
    if #cards > 0 then
      room:askToYiji(player, {cards = cards, skill_name = skel.name, min_num = 0, max_num = 999, prompt = "#rmt__dianyi-give", expand_pile = cards})
    end
  end
end

skel:addEffect("active", {
  anim_type = "control",
  prompt = "#rmt__dianyi",
  card_num = 0,
  target_num = 0,
  derived_piles = "rmt__dianyi",
  target_tip = function (self, player, to_select, selected, selected_cards, card, selectable, extra_data)
    if player:getHandcardNum() == to_select:getHandcardNum() then
      return "@@rmt__dianyi_tar"
    end
  end,
  interaction = function (self, player)
    local choices = {"draw1"}
    if table.find(Fk:currentRoom().alive_players, function (p)
      return p:getHandcardNum() == player:getHandcardNum() and not p:isNude()
    end) then
      table.insert(choices, "rmt__dianyi_throw")
    end
    return UI.ComboBox { choices = choices, all_choices = {"draw1", "rmt__dianyi_throw"} }
  end,
  times = function (self, player)
    return 1 - player:usedEffectTimes(skel.name, Player.HistoryPhase)
  end,
  can_use = function(self, player)
    return player:usedEffectTimes(skel.name, Player.HistoryPhase) == 0
  end,
  card_filter = Util.FalseFunc,
  on_use = function(self, room, effect)
    local player = effect.from
    doDianyi(player, self.interaction.data)
  end,
})

skel:addEffect(fk.Damaged, {
  anim_type = "masochism",
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(skel.name)
  end,
  trigger_times = function (self, event, target, player, data)
    return data.damage
  end,
  on_cost = function(self, event, target, player, data)
    local success , dat = player.room:askToUseActiveSkill(player, {
      skill_name = "rmt__dianyi", prompt = "#rmt__dianyi", cancelable = true, skip = true
    })
    if success and dat then
      event:setCostData(self, dat)
      return true
    end
  end,
  on_use = function (self, event, target, player, data)
    doDianyi(player, event:getCostData(self).interaction)
  end,
})

return skel
